//
//  ChatImageCell.m
//  YouinLiveDemo
//
//  Created by lprison on 2021/9/5.
//

#import "ChatImageCell.h"


@implementation ChatImageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self iniUI];
    }
    return self;
}

-(void)iniUI{
//    self.avatar = [UIImageView new];
//    self.avatar.layer.cornerRadius = 20;
//    self.avatar.layer.masksToBounds = YES;
    
    self.nameLabel = [UILabel new];
    self.nameLabel.font = [UIFont systemFontOfSize:12];
    
//    self.timeLabel = [UILabel new];
//    self.timeLabel.font = [UIFont systemFontOfSize:11];
//    self.timeLabel.textColor = [UIColor grayColor];
    
    self.contentView.backgroundColor = [UIColor clearColor];
    self.backgroundColor = [UIColor clearColor];
//    self.content = [UIView new];
//    self.content.backgroundColor = rgba(0, 0, 0, 0.3);
//    self.content.layer.cornerRadius = 12;
//    self.content.layer.masksToBounds = YES;
    
    self.image = [UIImageView new];
    self.image.contentMode = UIViewContentModeScaleAspectFit;
    self.image.layer.masksToBounds = true;
//    self.image.backgroundColor = [UIColor lightGrayColor];
    
//    [self.contentView addSubview:self.avatar];
    [self.contentView addSubview:self.nameLabel];
//    [self.contentView addSubview:self.timeLabel];
//    [self.contentView addSubview:self.content];
    [self.contentView addSubview:self.image];
    
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
//    [self.avatar mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.height.mas_equalTo(40);
//        make.left.mas_equalTo(self.contentView).offset(16);
//        make.top.mas_equalTo(self.contentView).offset(5);
//    }];
//
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(16);
        make.top.mas_equalTo(self.contentView).offset(5);
        
    }];
    
//    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(self.contentView).offset(-16);
//        make.top.mas_equalTo(self.avatar);
//
//    }];
    
//    [self.content mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(self.nameLabel);
//        make.left.mas_equalTo(self.nameLabel.mas_right).offset(10);
//        make.bottom.mas_equalTo(self.contentView).offset(-5);
//    }];
    
//    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.width.mas_equalTo(100);
//        make.edges.mas_equalTo(self.content);
//    }];
    
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.nameLabel.mas_right).offset(5);
        make.top.mas_equalTo(self.nameLabel).offset(5);
        make.height.width.mas_equalTo(75);
        make.bottom.mas_equalTo(self.contentView).offset(5);
        make.width.lessThanOrEqualTo(self.contentView.mas_width).offset(-42);
    
    }];
   
    
        
    [self layoutIfNeeded];
    
    //[self.content addCorner:UIRectCornerTopRight|UIRectCornerBottomLeft|UIViewContentModeBottomRight radius:5];

}
-(CGSize)GetImageSizeWithURL:(id)imageURL
{

    NSURL* URL = nil;

    if([imageURL isKindOfClass:[NSURL class]]){

    URL = imageURL;

    }

    if([imageURL isKindOfClass:[NSString class]]){

    URL = [NSURL URLWithString:imageURL];

    }

    NSData *data = [NSData dataWithContentsOfURL:URL];

    UIImage *image = [UIImage imageWithData:data];

    return CGSizeMake(image.size.width, image.size.height);

}
-(void)setMessage:(BaseMessageModel *)message{
    _message = message;
//    [self.avatar sd_setImageWithURL:[NSURL URLWithString:message.avatar]];
    self.nameLabel.text = message.nickName;
//    self.timeLabel.text = [self timestampSwitchTime:message.sendTime andFormatter:@"YYYY-MM-dd hh:mm:ss"];
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@：%@",message.nickName,message.text ? message.text : @""]];
    NSRange range1 = [[attr string] rangeOfString:message.nickName];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0,attr.length)];
    
    [attr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0,attr.length)];
    
    [attr addAttribute:NSForegroundColorAttributeName value:message.color range:range1];

    self.nameLabel.attributedText = attr;
    
    if(message.imageUrl){
        
        
       
//        [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
//
//            make.left.mas_equalTo(self.nameLabel.mas_right).offset(5);
//            make.top.mas_equalTo(self.nameLabel).offset(5);
//            make.height.width.mas_equalTo(100);
//            make.bottom.mas_equalTo(self.contentView).offset(5);
//            make.width.lessThanOrEqualTo(self.contentView.mas_width).offset(-42);
//
//        }];
        
        [self.image sd_setImageWithURL:[NSURL URLWithString:message.imageUrl]];
    }else{
        self.image.image = message.image;
    }
    
    
    [self layoutIfNeeded];
}

- (NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
    
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    if ([format isEqual:@"HH:mm:ss"] && confromTimespStr.length<8) {
        confromTimespStr = @"00:00:00";
    }
    if ([format isEqual:@"YYYY-MM-dd HH:mm:ss"] && confromTimespStr.length<18) {
        confromTimespStr = @"2000-01-01 00:00:00"; //默认返回
    }
    return confromTimespStr;
}


@end
